package cn.stylefeng.guns.modular.cb.controller;

import cn.stylefeng.guns.modular.cb.service.OSSService;
import cn.stylefeng.roses.kernel.model.response.ResponseData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

@RestController
@RequestMapping("/api/oss")
public class OSSController {

    private static Logger logger = LoggerFactory.getLogger(OSSController.class);

    @Autowired
    private OSSService ossService;

    @PostMapping("/upload_file")
    public ResponseData uploadFile(MultipartFile file){
        String fileId = ossService.uploadOssFile(file);
        logger.info("fileId: {}", fileId);
        if (!StringUtils.isEmpty(fileId)){
            fileId = fileId.substring(fileId.lastIndexOf('/')+1);
        }
        return ResponseData.success(fileId);
    }

    @RequestMapping("/download_file2")
    public ResponseData downloadFile2(String fileName, String LocalFileName){
        ossService.downloadFile(fileName, LocalFileName);
        return ResponseData.success();
    }

//    @RequestMapping("/download_file")
//    public byte[] downloadFile(String fileId, HttpServletResponse response){
//        byte[] bytes = ossService.streamFile(fileId, response);
//        return bytes;
//    }

    @RequestMapping("/download_file")
    public void downloadFile(String fileId, HttpServletResponse response){
        byte[] bytes = ossService.streamFile(fileId, response);
        try (ServletOutputStream servletOutputStream = response.getOutputStream();
             InputStream is = new ByteArrayInputStream(bytes);){
            response.setContentLength(bytes.length);
            byte[] buffer = new byte[5120];
            int len = 0;
            while ((len = is.read(buffer)) > 0) {
                servletOutputStream.write(buffer, 0, len);
            }
            servletOutputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 显示文件
     * @param objectName
     * @param response
     */
    @RequestMapping("/stream_file")
    public void streamFile(String objectName, HttpServletResponse response){
        byte[] bytes = ossService.streamFile(objectName, response);
        try (ServletOutputStream servletOutputStream = response.getOutputStream();
             InputStream is = new ByteArrayInputStream(bytes);){
            response.setContentLength(bytes.length);
            byte[] buffer = new byte[2048];
            int len = 0;
            while ((len = is.read(buffer)) > 0) {
                servletOutputStream.write(buffer, 0, len);
            }
            servletOutputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @RequestMapping("/delete_file")
    public void deleteFile(String fileId){
        ossService.delImg(fileId);
    }
}
